package com.example.fish.controller;

import com.example.fish.dto.UserDTO;
import com.example.fish.entity.User;
import com.example.fish.service.UserService;
import jakarta.validation.Valid;
import jakarta.validation.groups.Default;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {

    private final UserService userService;

    /**
     * 新增用户
     */
    @PostMapping
    public UserDTO create(@RequestBody
                          @Validated({UserDTO.Create.class, Default.class})
                          UserDTO dto) {
        User user = new User();
        BeanUtils.copyProperties(dto, user);
        userService.save(user);
        // 回填数据库生成的 id、时间等
        BeanUtils.copyProperties(user, dto);
        dto.setPassword(null); // 出于安全考虑可隐藏密码
        return dto;
    }
    @PostMapping("/login")

    public Map<String, Object> login(@RequestBody Map<String, String> payload) {
        String account = payload.get("account");
        String password = payload.get("password");

        User user = userService.getByAccount(account);
        Map<String, Object> result = new HashMap<>();
        if (user == null) {
            result.put("success", false);
            result.put("message", "账号不存在");
            return result;
        }
        if (!user.getPassword().equals(password)) {
            result.put("success", false);
            result.put("message", "密码错误");
            return result;
        }
        if (!"正常".equals(user.getStatus())) {
            result.put("success", false);
            result.put("message", "账号状态：" + user.getStatus());
            return result;
        }
        // 正常登录
        result.put("success", true);
        result.put("user", Map.of(
                "id", user.getIdUser(),
                "account", user.getAccount(),
                "nickname", user.getNickname()
        ));
        return result;
    }

    /**
     * 按账号查询单个用户
     */
    @GetMapping("/account/{account}")
    public UserDTO getByAccount(@PathVariable String account) {
        User user = userService.getByAccount(account);
        if (user == null) return null;
        UserDTO dto = new UserDTO();
        BeanUtils.copyProperties(user, dto);
        dto.setPassword(null);
        return dto;
    }

    /**
     * 查询所有用户
     */
    @GetMapping
    public List<UserDTO> listAll() {
        return userService.list().stream().map(user -> {
            UserDTO dto = new UserDTO();
            BeanUtils.copyProperties(user, dto);
            dto.setPassword(null);
            return dto;
        }).collect(Collectors.toList());
    }

    /**
     * 更新用户状态
     */
    @PatchMapping("/{id}/status")
    public boolean updateStatus(@PathVariable Integer id,
                                @RequestParam String status) {
        return userService.updateStatus(id, status);
    }
}
